package br.com.tecnove.random.android;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RPGActivity extends Activity implements View.OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rpglayout);

		Button bLessDices = (Button) findViewById(R.id.btnLessDices);
		Button bLessRolls = (Button) findViewById(R.id.btnLessRolls);
		Button bPlusDices = (Button) findViewById(R.id.btnPlusDices);
		Button bPlusRolls = (Button) findViewById(R.id.btnPlusRolls);

		bLessDices.setOnClickListener(this);
		bLessRolls.setOnClickListener(this);
		bPlusDices.setOnClickListener(this);
		bPlusRolls.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnLessDices:
			changeValue(false, (EditText) findViewById(R.id.editTextDices));
			break;
		case R.id.btnLessRolls:
			changeValue(false, (EditText) findViewById(R.id.editTextRolls));
			break;
		case R.id.btnPlusDices:
			changeValue(true, (EditText) findViewById(R.id.editTextDices));
			break;
		case R.id.btnPlusRolls:
			changeValue(true, (EditText) findViewById(R.id.editTextRolls));
			break;
		default:
			Log.e("RPGActivity", "ID not found on onClick");
		}
	}

	private void changeValue(boolean isIncrease, EditText eText) {
		try {
			int actualValue = Integer.parseInt(eText.getText().toString());
			if (isIncrease)
				eText.setText(String.valueOf(actualValue + 1));
			else
				eText.setText(String.valueOf(actualValue - 1));
		} catch (NumberFormatException e){
			Toast.makeText(this, getString(R.string.badInputFormat), Toast.LENGTH_LONG).show();
			Log.w("RPGActivity", "NumberFormat eText("+eText.toString()+") "+e.getMessage());
		}		
	}
}
